package com.hxzy.controller.front;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.R;
import com.hxzy.dto.front.ShopResubmitDTO;
import com.hxzy.entity.Merchandise;
import com.hxzy.entity.Merchandiser;
import com.hxzy.service.MerchandiseService;
import com.hxzy.service.MerchandiserService;
import com.hxzy.vo.front.MerchandiserProcessVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author wy
 * @Description 前端商家信息
 * @Date 2023/4/18 15:20
 * @Version 1.0
 */
@Api(tags = "前端商家API")
@RestController
@RequestMapping(value = "/front/enterprise")
public class FrontMerchandiseController  extends BaseController {


    @Autowired
    private MerchandiserService  merchandiserService;


    @ApiOperation(value = "商家注册")
    @PostMapping(value = "/add")
    public R register(@RequestBody @Valid Merchandiser entity){
        // 验证号码是否已经注册过了
        boolean exists = this.merchandiserService.existsLoginName(entity.getLoginName());
        if(exists){
            return R.build(AckCode.EXISTS_VALUE_ACCOUNT);
        }

        entity.setLoginPwd(BCrypt.hashpw(entity.getLoginPwd()));
        entity.setStatus((short) 1);
        entity.setCreateBy(entity.getLoginName());
        entity.setCreateTime(new Date());

        int count = this.merchandiserService.insert(entity);
        return super.toR(count);
    }

    @ApiOperation(value = "商家重新提交申请资料")
    @PutMapping(value = "/update")
    public R resubmitRegister(@RequestBody @Valid ShopResubmitDTO entity){
        // 验证商家的手机号必须存在，并且审核状态是未通过才能
        Merchandiser db = this.merchandiserService.existsLoginNameAndNoPassed(entity.getLoginName());
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        //为了防止用户恶意攻击你
        if(db.getId().intValue()!=entity.getId().intValue()){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        //复制修改的数据
        BeanUtils.copyProperties(entity, db);

        db.setStatus((short) 1);
        db.setUpdateTime(new Date());
        db.setUpdateBy(entity.getLoginName());
        int count = this.merchandiserService.updateByPrimaryKeySelective(db);
        return super.toR(count);
    }




    @GetMapping(value = "/process/{loginName}")
    @ApiOperation(value = "查询商家审核历史记录")
    public R findProcessByLoginName(@PathVariable(value = "loginName") String loginName){
        if(StrUtil.isBlank(loginName)){
            return R.build(AckCode.PHONE_NUMBER_NOTNULL);
        }
        //验证电话号码有效性
        String phoneExg="^0?(13|15|17|18|14|19)[0-9]{9}$";
        //定义一个正则表达式
        Pattern pattern = Pattern.compile(phoneExg);
        //创建一个匹配器，匹配正则表达式
        Matcher matcher = pattern.matcher(loginName);
        if(!matcher.matches()){
            return R.build(AckCode.PHONE_NUMBER_NOTNULL);
        }

        //查询数据
        MerchandiserProcessVO merchandiserProcessVO=this.merchandiserService.findProcessByLoginName(loginName);
        if(merchandiserProcessVO==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(merchandiserProcessVO);
    }

}
